package main

import (
	"fmt"
	"time"
)

//学生班级 学号
type Student struct {
	Class int
	Nums  int
}

//老师发卷子
func teacher(teacherId int, stu <-chan Student, complete chan<- bool) {
	for s := range stu {
		fmt.Println("Teacher", teacherId, "distributes paper to class:", s.Class, "nums:", s.Nums, "student")
		time.Sleep(time.Second)
		//time.Sleep(time.Millisecond)
		fmt.Println("Class:", s.Class, "nums:", s.Nums, "student gets paper")
		complete <- true
	}
}

func main() {
	start := time.Now()

	paper := make(chan Student, 400)
	complete := make(chan bool, 400)

	//每个老师一个协程，开六个协程
	for i := 1; i <= 6; i++ {

		go teacher(i, paper, complete)

	}

	//写入学生信息
	for class := 1; class <= 10; class++ {
		for nums := 1; nums <= 40; nums++ {
			var tmp Student
			tmp.Class = class
			tmp.Nums = nums
			paper <- tmp
		}

	}

	close(paper)

	for a := 1; a <= 400; a++ {
		<-complete
	}

	end := time.Now().Sub(start).Seconds()
	fmt.Println("老师发卷子完成共耗时：", int(end), "秒")
}
